Discrete Cosine Transform


Discrete Cosine Transform, DCT

It is a transformation used on image compression that expresses a sequence of values as a sum of cosine functions.
Es una transformación usada en compresión de imágenes que expresa una secuencia de valores cómo la suma de funciones coseno.

Problem 1
Create a program called CosTest to compute the DCT by using the direct implementation of the DCT equation and a fast method for eight points.
Cree un programa llamado CosTest para calcular la DCT usando una implementación directa de la ecuación DCT y un método rápido para ocho puntos.

CosTestRun

CosTest.h
#pragma once //______________________________________ CosTest.h
#include "Resource.h"

class CosTest: public Win::Dialog
{
public:
     CosTest()
     {
     }
     ~CosTest()
     {
     }
     void DiscreteCosineTransform(const float* input, float* output, const size_t N);
     void InverseDiscreteCosineTransform(const float* input, float* output, const size_t N);
     void Display(const float* data, Win::Textbox& tbx);
     . . .
};


CosTest.cpp
. . .
void CosTest::Window_Open(Win::Event& e)
{
     //______________________________________________________________ 1. Variable
     const float input[] ={2.2f, 5.87f, -3.45f, 11.23f, -10.23f, 8.7f, 9.3f, 3.4f};;
     float output[8];
     float restored[8];
     //______________________________________________________________ 2. Direct implementation
     DiscreteCosineTransform(input, output, 8);
     InverseDiscreteCosineTransform(output, restored, 8);
     Display(input, tbxInput);
     Display(output, tbxDirectDCT);
     Display(restored, tbxDirectRestored);
     //______________________________________________________________ 3. Fast DCT
     ::memcpy(output, input, 8*sizeof(float));
     Math::DiscreteCosineT dc;
     dc.Transform(output, true, 1);
     Display(output, tbxFastDCT);
     dc.Inverse(output, true, 1);
     Display(output, tbxFastRestored);
}

void CosTest::DiscreteCosineTransform(const float* input, float* output, const size_t N)
{
     // https://en.wikipedia.org/wiki/Discrete_cosine_transform#DCT-II
     size_t n = 0, k = 0;
     const float* in;
     const float a = (float)M_PI/(float)N;
     for (k = 0; k < N; k++, output++)
     {
          in = input;
          *output = 0.0f;
          for (n = 0; n < N; n++, in++)
          {
               (*output) += (*in)*cos(a*(n + 0.5f)*(float)k);
          }
          if (k == 0) (*output) /= sqrt(2.0f);
          (*output) /= 2.0f;
     }
}

void CosTest::InverseDiscreteCosineTransform(const float* input, float* output, const size_t N)
{
     // https://en.wikipedia.org/wiki/Discrete_cosine_transform#DCT-III
     size_t n = 0, k = 0;
     const float* in;
     const float a = (float)M_PI/(float)N;
     const float b = 4.0f/(float)N;
     for (k = 0; k < N; k++, output++)
     {
          in = input;
          *output = (*in) / sqrt(2.0f);
          in++;
          for (n = 1; n < N; n++, in++)
          {
               (*output) += (*in)*cos(a*(float)n*(k + 0.5f));
          }
          *output *= b;
     }
}

void CosTest::Display(const float* data, Win::Textbox& tbx)
{
     wchar_t tmp[32];
     wstring text;
     for (int i = 0; i < 8; i++)
     {
          _snwprintf_s(tmp, 32, _TRUNCATE, L"%.3f, ", data[i]);
          text += tmp;
     }
     tbx.Text = text;
}


© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home